﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Framework.Helper;
using System.Data;
using CNVP.Framework.Utils;
using CNVP.Framework;

namespace CNVP.JShop.Web.Member
{
    public partial class OrderList : UserPage
    {
        BLL.CNVP_Order bll = new BLL.CNVP_Order();
        Model.CNVP_Order model = new Model.CNVP_Order();
        public int rplistCount = 0;
        protected string olbid = "-1";
        int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["olbid"]))
            {
                olbid = Request.QueryString["olbid"].ToString();
            }
            if (!IsPostBack)
            {
                OrderState();
                StartLoad(PageIndex);
            }
        }

        /// <summary>
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dt = null;
            int pageNum = 20;
            SQLConditionInfo[] st = new SQLConditionInfo[4];
            st[0] = new SQLConditionInfo("@olbid", olbid);
            st[1] = new SQLConditionInfo("@oUserAdminType", "0");
            st[2] = new SQLConditionInfo("@oBuyerID", CookieUserID);
            st[3] = new SQLConditionInfo("@key", Public.FilterSql(txtkey.Text.Trim()));
            dt = bll.GetPage(this.GetType().Name, PageIndex, pageNum, out i, out j, st);
           
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    rplistCount = dt.Rows.Count;//行数
                    //----------------------------------------添加列------------------------------------------------
                    dt.Columns.Add("oID", typeof(string));
                    dt.Columns.Add("oOrderNum", typeof(string));
                    dt.Columns.Add("oOrderProductsImg", typeof(string));
                    dt.Columns.Add("oOrderTotalAmount", typeof(string));
                    dt.Columns.Add("oGetTrueName", typeof(string));
                    dt.Columns.Add("oOrderState", typeof(string));
                    dt.Columns.Add("oOrderStateNO", typeof(string));
                    dt.Columns.Add("oPayWay", typeof(string));
                    //----------------------------------------添加列结束--------------------------------------------
                    for (int k = 0; dt.Rows.Count > k; k++)
                    {
                        dt.Rows[k]["oID"] = dt.Rows[k]["ID"].ToString();
                        dt.Rows[k]["oOrderNum"] = dt.Rows[k]["OrderNum"].ToString();
                        dt.Rows[k]["oOrderProductsImg"] = OrderProductsImg(Convert.ToInt32(dt.Rows[k]["ID"].ToString()));
                        dt.Rows[k]["oOrderTotalAmount"] = dt.Rows[k]["OrderTotalAmount"].ToString();
                        dt.Rows[k]["oGetTrueName"] = dt.Rows[k]["GetTrueName"].ToString();
                        dt.Rows[k]["oOrderState"] = BLL.BLLOrder.getorderztClint(int.Parse(dt.Rows[k]["OrderState"].ToString()));
                        dt.Rows[k]["oOrderStateNO"] = dt.Rows[k]["OrderState"].ToString();//数字
                        dt.Rows[k]["oPayWay"] = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(int.Parse(dt.Rows[k]["PayWay"].ToString())).ColumnName;
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        private void OrderState()
        {
            string html = "订单状态：";
            string[] orderzt = { "等待付款", "等待对方发货", "配送中", "", "确认收货", "", "", "", "退货单", "已取消" };
            if (olbid == "-1")
            {
                html += "<b><a href='orderlist.aspx' style='COLOR: #009966\'>所有订单</a></b> ";
            }
            else
            {
                html += "<a href='orderlist.aspx'>所有订单</a> ";
            }
            for (int i = 0; i < 10; i++)
            {
                if (i.ToString() == olbid)
                {
                    html += "<b><a href='orderlist.aspx?olbid=" + i + "' style='COLOR: #009966\'>" + orderzt[i] + "</a></b> ";
                }
                else
                {
                    html += "<a href='orderlist.aspx?olbid=" + i + "'>" + orderzt[i] + "</a> ";
                }
            }
            if (olbid == "5,6,7")
            {
                html += "<b><a href='orderlist.aspx?olbid=5,6,7' style='COLOR: #009966\'>已完成</a></b> ";
            }
            else
            {
                html += "<a href='orderlist.aspx?olbid=5,6,7'>已完成</a>";
            }
            OLb.Text = html;
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            StartLoad(PageIndex);
        }

        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
             //方法一
            if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
            {
                Label lbOrderStateNO = (Label)e.Item.FindControl("lbOrderStateNO");
                LinkButton linkBTCanel = (LinkButton)e.Item.FindControl("linkBTCanel");
                LinkButton linkBTOK = (LinkButton)e.Item.FindControl("linkBTOK");
                linkBTCanel.Attributes.Add("onclick", "return confirm('确定取消吗?');");
                linkBTOK.Attributes.Add("onclick", "return confirm('确定确认收货吗?');");
                linkBTCanel.Visible = false;
                linkBTOK.Visible = false;
                if (lbOrderStateNO.Text == "0")
                {
                    linkBTCanel.Visible = true;
                }
                if (lbOrderStateNO.Text == "3")
                {
                    linkBTOK.Visible = true;
                }
            }
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            if (e.CommandName == "Canel")
            {
                model.ID = id;
                model.OrderState = 9;
                bll.UpdateOrderState(model);
            }
            if (e.CommandName == "OK")
            {
                model.ID = id;
                model.OrderState = 4;
                bll.UpdateOrderState(model);
            }
            StartLoad(PageIndex);
        }

        private string OrderProductsImg(int OrderID)
        {
            string html = string.Empty;
            BLL.CNVP_OrderProducts bll_OrderProducts = new BLL.CNVP_OrderProducts();
            var list = bll_OrderProducts.GetAll_CNVP_OrderProducts(OrderID);
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    html += "<li class=\"MyReviewPro\">";
                    html += "<a href=\"/ProductDetail_" + list[i].ProductClassID + "_" + list[i].ProductID + ".aspx\" target=\"_blank\" title=\"" + list[i].ProductName + "\">";
                    html += "   <img src=\"/UploadFile/Product/" + list[i].ImgProLoad + "\" height=\"40px\" width=\"40px\" />";
                    html += "</a>";
                    html += "</li>";
                }
            }
            return html;
        }
    }
}